package net.privatenav.smsonpc;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Enumeration;

import android.os.Bundle;
import android.provider.CalendarContract.Events;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.telephony.SmsManager;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class MainActivity extends Activity {
		
	String _Hostname= null;
	String _Local = null;
	int _Port = 8888;
	
	private Socket _s;
	private Thread _readingThread;
	private Events _events;
	
	public Boolean StartServices(String Host, int Port)
	{
		try {
			_s = new Socket(Host, Port);
			_readingThread = new Thread(_readingRun);
			_readingThread.start();
		    } catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		    } catch (IOException e) {
			e.printStackTrace();
			return false;
		    }
		return true;
	}
	
	private Runnable _readingRun = new Runnable()
	{
	    public void run()
            {
		try {
		    InputStream input;
		    input = _s.getInputStream();
	            int readBytes;
	            byte[] buffer = new byte[1024];
	            while ((readBytes = input.read(buffer, 0, 1024)) >= 0)
	            {
	                if (readBytes > 0)
	                {
	                    String _receivedStr = new String(buffer, 0, readBytes, "UTF-8");
	                    // ...
	                }
	                else
	                    break;
	            }
                    _s.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	};
	
	public void StopServices()
	{
		
	}
	
	public String alert()
	{
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("IP Hostname");
		alert.setMessage("Enter the IP Adress of your computer");

		// Set an EditText view to get user input 
		final EditText input = new EditText(this);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		//SI C'EST OK	
			_Hostname = input.getText().toString();
			Toast.makeText(getApplicationContext(),_Hostname,
					Toast.LENGTH_SHORT).show();
			CheckBox CheckBoxWF = ( CheckBox ) findViewById( R.id.checkBox2 );
			CheckBoxWF.setText("On");
			//ici on lancera les sockets
			if (StartServices(_Hostname, _Port) == true)
			{
				TextView Message = (TextView)findViewById(R.id.textView4);
				Message.setText("You are now connected");
			}
			else 
			{
				TextView Message = (TextView)findViewById(R.id.textView4);
				Message.setText("Cannot be connected, something's wrong");
			}
		  }
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
		  //SI C'EST PAS BON
			  Toast.makeText(getApplicationContext(),"Operation aborted, can't connect, please try again",
						Toast.LENGTH_LONG).show();
			  CheckBox CheckBoxWF = ( CheckBox ) findViewById( R.id.checkBox2 );
				CheckBoxWF.setChecked(false);
		  }
		});

		alert.show();
		
		return _Hostname;
	}
	
	public void Send()
	  {
	    Button btnSend = (Button)findViewById(R.id.button1);
	    btnSend.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String phoneNo = "0667843403";
    String message = "ok so this is a fucking test";    

			  try {
				SmsManager smsManager = SmsManager.getDefault();
				smsManager.sendTextMessage(phoneNo, null, message, null, null);
				Toast.makeText(getApplicationContext(), "SMS Sent!",
							Toast.LENGTH_LONG).show();
			  } catch (Exception e) {
				Toast.makeText(getApplicationContext(),
					"SMS faild, please try again later!",
					Toast.LENGTH_LONG).show();
				e.printStackTrace();
			  }
				
			}
		});
	    
	  }
	
	public String GetIpAdress()
	{
		return null;
	}
	
	@Override
	protected void onStart() {
		CheckBox CheckBoxWF = ( CheckBox ) findViewById( R.id.checkBox2 );
		CheckBoxWF.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		        if ( isChecked )
		        {
		            alert();
		            
		        }
		        else 
		        {
		        	
		        }

		    }
		});
		super.onStart();
	}
	
	  
	  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Send();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
